package cn.stylefeng.guns.sys.modular.system.service.impl;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.pojo.node.LayuiTreeNode;
import cn.stylefeng.guns.base.pojo.node.ZTreeNode;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.system.entity.BtyPinpai;
import cn.stylefeng.guns.sys.modular.system.mapper.BtyPinpaiMapper;
import cn.stylefeng.guns.sys.modular.system.model.params.BtyPinpaiParam;
import cn.stylefeng.guns.sys.modular.system.model.result.BtyPinpaiResult;
import cn.stylefeng.guns.sys.modular.system.service.BtyPinpaiService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 装修科目表 服务实现类
 * </p>
 *
 * @author gyz
 * @since 2020-11-02
 */
@Service
public class BtyPinpaiServiceImpl extends ServiceImpl<BtyPinpaiMapper, BtyPinpai> implements BtyPinpaiService {

    @Override
    public void add(BtyPinpaiParam BtyPinpaiParam) {
        BtyPinpaiParam.setCreateUser(LoginContextHolder.getContext().getUserId());

        BtyPinpai entity = getEntity(BtyPinpaiParam);
        this.save(entity);
    }


    @Override
    public void delete(BtyPinpaiParam param) {
        this.removeById(getKey(param));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(BtyPinpaiParam param) {
        BtyPinpai oldEntity = getOldEntity(param);
        BtyPinpai newEntity = getEntity(param);
        newEntity.setUpdateUser(LoginContextHolder.getContext().getUserId());
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }



    @Override
    public List<BtyPinpaiResult> findListBySpec(BtyPinpaiParam param) {
        return this.baseMapper.findListBySpec(param);
    }

    @Override
    public LayuiPageInfo findPageBySpec(BtyPinpaiParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(BtyPinpaiParam param) {
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private BtyPinpai getOldEntity(BtyPinpaiParam param) {
        return this.getById(getKey(param));
    }

    private BtyPinpai getEntity(BtyPinpaiParam param) {
        BtyPinpai entity = new BtyPinpai();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }



    @Override
    public BtyPinpai getDetailById(Long btyBtyPinpaiId) {
        BtyPinpai detail = this.getById(btyBtyPinpaiId);
        return detail;
    }
}
